(:
  A procedure to select the contents of the 2nd worksheet in excel
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
    
declare namespace xcl = "urn:schemas-microsoft-com:office:spreadsheet";

declare function local:get-timestamp($datecell as node(),$timetype as xs:string) as xs:integer
{
let $time := xs:time(string($datecell))
let $date := fn:dateTime(xs:date('2006-08-15'),$time)
return 
jj:date-to-millis($date)
};

<display>{
let $t := $arguments[1]
let $idxarray := for $string in fn:tokenize($arguments[2],",") return xs:integer($string)
let $cols := fn:tokenize($arguments[3],",")
let $start := if (xs:integer($arguments[4])=0) then 1 else xs:integer($arguments[4])
let $end := if (xs:integer($arguments[5])=0) then 100000 else xs:integer($arguments[5])
let $timetype := $arguments[6]
let $d := doc($t)/xcl:Workbook/xcl:Worksheet[position()=$idxarray]
let $rows := $d/xcl:Table/xcl:Row[position()<=$end][position()>=$start]
let $times := for $row at $p in $rows
		return local:get-timestamp($row/xcl:Cell[1],$timetype)
for $row at $p in $rows
    let $time := $times[$p]
		let $millis := $time
		return
	  <item>{
    		<info name="time"><time><date>{$millis}</date><duration>2</duration></time></info>,
    		<info name="src-anchor">{
      		<anchor>{
        		<doc>{ $t }</doc>,
        		<path>{jj:build-Path($row)}</path>
      	  }</anchor>
    		}</info>,
    		for $col at $colp in $cols 
    		let $s := string($row/xcl:Cell[$colp])
    		let $nonnullstring := if ($s) then $s else "_"
    		where $col!="time"
    		return <info name="{$col}">{$nonnullstring}</info>
    		}</item>
}</display>
